package com.xsd.awen.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsd.awen.config.WebResult;
import com.xsd.awen.domain.entity.User;
import com.xsd.awen.domain.po.UserPagePo;
import com.xsd.awen.domain.vo.UserVo;
import com.xsd.awen.mapper.UserMapper;
import com.xsd.awen.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

@Service
@Scope(proxyMode= ScopedProxyMode.TARGET_CLASS)
public class UserService {

    @Autowired
    UserMapper userMapper;

    public PageInfo<UserPagePo> page(UserVo userVo){
        PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        return new PageInfo<>(userMapper.page(userVo));
    }

    public String save(User user){
        Integer count = userMapper.countUser(user);
        if( count != null && count > 0 ){
            return WebResult.ERROR("用户名重复");
        }
        user.setId(UUIDUtils.generate());
        userMapper.insert(user);
        return WebResult.OK();
    }

    public String update(User user){
        Integer count = userMapper.countUpdateUser(user);
        if( count != null && count > 0 ){
            return WebResult.ERROR("用户名重复");
        }
        userMapper.updateById(user);
        return WebResult.OK();
    }

    public String delete(User user){
        userMapper.deleteById(user);
        return WebResult.OK();
    }

    public User getById(String id){
        User user = userMapper.selectById(id);
        return user;
    }

    public User getUserByUsername(User user){
        return userMapper.getUserByUsername(user);
    }


}
